package com.example.logindb.controller;


import com.example.logindb.dto.ResponseMessage;
import com.example.logindb.dto.UserDTO;
import com.example.logindb.entity.Help;
import com.example.logindb.entity.HelpList;
import com.example.logindb.entity.Location;
import com.example.logindb.entity.Online;
import com.example.logindb.sevices.HelpService;
import com.example.logindb.sevices.LocationService;
import com.example.logindb.sevices.OnlineService;
import com.example.logindb.sevices.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;


@RestController
@RequestMapping("/help")
public class HelpController {

    private HelpService helpService;
    private OnlineService onlineService;
    private LocationService locationService;
    private UserService userService;
    @Autowired
    public HelpController(HelpService helpService, OnlineService onlineService, LocationService locationService, UserService userService) {
        this.helpService = helpService;
        this.onlineService = onlineService;
        this.locationService = locationService;
        this.userService = userService;
    }

    //盲人发起求助
    @PostMapping
    public ResponseMessage<Help> waitForHelp(
            @RequestBody Location location)
    {
        //更新在线状态为等待帮助
        Online online = new Online();
        online.setId(location.getLocid());
        online.setStatus(3);
        onlineService.updateStatus(online);
        //添加帮助活动
        Help help = new Help();
        Location location1 = locationService.GPSToAMap(location);
        help.setBid(location.getLocid()); //获取盲人位置、id
        help.setLon(location1.getLon());
        help.setLat(location1.getLat());
        help.setBtime(Timestamp.valueOf(LocalDateTime.now()));
        help.setAddress(locationService.getAddress(location1.getLon(), location1.getLat()));
        helpService.blindWaitforHelp(help);
        return ResponseMessage.success(help);
    }

    //查询附近帮助需求
    @GetMapping("/volunteer")
    @ResponseBody
    public List<HelpList> getBlind(
            @RequestParam Double lat,
            @RequestParam Double lon,
            @RequestParam Integer id
    )
    {   
        Location location = new Location();
        location.setLon(lon);
        location.setLocid(id);
        location.setLat(lat);
        locationService.updateLocation(location);
        List<Help> helps = helpService.findBlindByLoc(lat,lon);
        List<HelpList> helplists = new ArrayList<>();
        if(!helps.isEmpty()){
            for (Help help : helps) {
                HelpList helplist = new HelpList();
                helplist.setAid(help.getAid());
                helplist.setBid(help.getBid());
                helplist.setBtime(help.getBtime());
                helplist.setAddress(help.getAddress());
                helplist.setDistance(helpService.getDistance(help.getLon(), help.getLat(), lon, lat));
                helplist.setLat(help.getLat());
                helplist.setLon(help.getLon());
                helplists.add(helplist);
            }
        }
        return helplists;
    }
    //志愿者接单
    @GetMapping("/blind")
    @ResponseBody
    public ResponseMessage<String> helpBlind(
            @RequestParam Integer bid, //盲人id
            @RequestParam Integer vid, //志愿者id
            @RequestParam Integer aid //活动id
    )
    {
        //若没有匹配到志愿者，且盲人状态为等待帮助，则更新活动表单
        if(helpService.findAllByAid(aid).getVid()==0)
        {
            //活动表单上更新志愿者id
            Help help = new Help();
            help.setAid(aid);
            help.setVid(vid);
            help.setStatus(1);//正在帮助状态
            help.setStime(Timestamp.valueOf(LocalDateTime.now()));
            helpService.updateblind(help);
            //根据盲人id找到电话
            UserDTO blind = userService.findById(bid);
            //更新盲人和志愿者的状态
            onlineService.updateStatus(new Online(bid,2));//盲人正在被帮助
            onlineService.updateStatus(new Online(vid,2));//志愿者正在帮助
            return ResponseMessage.success(blind.getUsername());
        }
        return ResponseMessage.fail("请求超时");
    }
    //结束帮助
    @PostMapping("/end")
    public ResponseMessage endHelp(
            @RequestBody Help endhelp
    )
    {
        Help help = new Help();
        help.setAid(endhelp.getAid());
        help.setEtime(Timestamp.valueOf(LocalDateTime.now()));
        helpService.updateEnd(help);
        onlineService.updateStatus(new Online(help.getVid(),1));//志愿者帮助结束
        onlineService.updateStatus(new Online(help.getBid(),1));//盲人帮助结束
        return ResponseMessage.success(null);
    }
    //志愿者查自己的帮助记录
    @GetMapping("/volunteerhistory")
    public List<Help> getHistory(
            @RequestParam Integer vid
    )
    {
        return helpService.findByVid(vid);
    }

}
